﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Core.Common;

namespace Yoozoo.Gameplay.City
{

    public enum CityPointType
    {
        ExplorePoint = 1,           // 探索点
        RpgPoint = 10,              // RPG关卡位置
        RtsPoint = 20,              // RTS关卡位置
        BuildIndustry = 30,         // 建造产业位置
        InitStatus = 40,            // 初始化状态位置
        Fog = 50,                   // 迷雾位置
    }
    
    public class CityPointComponent : MonoBehaviour
    {
        [Header("点类型")]
        public CityPointType pointType;
        [Header("对应配表中的位置ID")]
        public int id;

#if UNITY_EDITOR
        
        private GameObject hud;
        
        private void OnEnable()
        {
            
        }

        private void OnDrawGizmos()
        {
            if (!CityPointEditorConfig.Instance.preview)
            {
                if (hud) GameObject.DestroyImmediate(hud); 
                return;
            }
            
            
            var config = CityPointEditorConfig.GetConfig(pointType);
            if (config == null) return;
            
            Gizmos.color = config.color;
            if (CityPointEditorConfig.Instance.previewMode == PointPreviewMode.Sphere)
            {
                if (hud)  GameObject.DestroyImmediate(hud);
                if (transform.childCount > 0)
                {
                    for (int i = 0; i < transform.childCount; i++)
                    {
                        GameObject.DestroyImmediate(transform.GetChild(i).gameObject);
                    }
                }
                Gizmos.DrawSphere(transform.position, config.radius);
            }
            else if (CityPointEditorConfig.Instance.previewMode == PointPreviewMode.Wire)
            {
                if (hud)  GameObject.DestroyImmediate(hud);
                if (transform.childCount > 0)
                {
                    for (int i = 0; i < transform.childCount; i++)
                    {
                        GameObject.DestroyImmediate(transform.GetChild(i).gameObject);
                    }
                }
                Gizmos.DrawWireSphere(transform.position, config.radius);
            }
            else if (CityPointEditorConfig.Instance.previewMode == PointPreviewMode.Hud)
            {
                if (!hud)
                {
                    if (config.hud)
                    {
                        if (transform.childCount > 0)
                        {
                            for (int i = 0; i < transform.childCount; i++)
                            {
                                GameObject.DestroyImmediate(transform.GetChild(i).gameObject);
                            }
                        }
                        
                        hud = GameObject.Instantiate(config.hud,this.transform);
                        hud.transform.localPosition = Vector3.zero;
                        hud.transform.localEulerAngles = CityPointEditorConfig.Instance.hudAngle;
                        hud.SetActive(true);
                    }
                }

            }
        }

        private void OnValidate()
        {
            transform.name = pointType.ToString() + "-" + id.ToString();
        }

#endif
        
    }
}